<script setup>
import { reactive, toRefs, h } from 'vue';
import { fields } from './index';

const state = reactive({
  visible: false,
  promptValue: '',
  type: 'confirm'
});

const props = defineProps({
  title: {
    type: String,
    default: 'Message'
  },
  content: {
    type: String,
    default: 'Message Content'
  },
  showCancelBtn: {
    type: Boolean,
    default: false
  },
  confirmBtnText: {
    type: String,
    default: 'OK'
  },
  cancelBtnText: {
    type: String,
    default: 'Cancel'
  },
  field: {
    type: String,
    default: 'confirm',
    //需要验证是否含有定义的字段,若没有会出现警告
    validator: (value) => {
      return fields.includes(value);
    }
  }
});

//将state解构出来的属性重新响应式包装
const { visible } = toRefs(state);

const setVisible = (isVisible) => {
  state.visible = isVisible;
};

const confirmBtnClick = () => {
  state.type = 'confirm';
  setVisible(false);
};

const cancelBtnClick = () => {
  state.type = 'cancel';
  setVisible(false);
};

//自定义视图模板 在视图模板里使用<content-view :field="field"></content-view>标签即可
//ContentView模板函数的参数是props 解构写法{ field }
const ContentView = ({ field }) => {
  switch (field) {
    // 没有field字段 或者 字段为 confirm时
    case !field || 'confirm':
      //标签 属性 子节点
      return h('p', null, props.content);
    case 'prompt':
      //返回带输入框的视图模板
      return h('input', {
        value: state.promptValue,
        onInput: (e) => (state.promptValue = e.target.value),
        class: 'message-input'
      });
    default:
      return '';
  }
};

//暴露当前 <script setup> 组件里的属性或方法供外界访问，如应用实例
defineExpose({ setVisible, state });
</script>

<template>
  <!-- 当使用 <transition> 作为根结点的组件会触发过渡动画效果 -->
  <transition name="my-ui-messagebox-fade">
    <!-- 遮罩层 -->
    <div class="ui-message-box" v-show="visible" @click="cancelBtnClick">
      <!-- 消息盒子 -->
      <div class="ui-message-box-wrapper" @click.stop>
        <div class="message-box-title">
          <h1>{{ title }}</h1>
          <span class="fa fa-close" @click="cancelBtnClick"></span>
        </div>
        <div class="message-box-content">
          <content-view :field="field"></content-view>
        </div>
        <div class="message-box-btn-group">
          <button class="btn btn-primary" @click="confirmBtnClick">
            {{ confirmBtnText }}
          </button>
          <button
            class="btn btn-default"
            @click="cancelBtnClick"
            v-if="showCancelBtn"
          >
            {{ cancelBtnText }}
          </button>
        </div>
      </div>
    </div>
  </transition>
</template>

<style lang="scss" scoped>
h1,
p {
  margin: 0;
  font-weight: normal;
}

.my-ui-messagebox-fade-enter-from,
.my-ui-messagebox-fade-leave-to {
  opacity: 0;
}

.my-ui-messagebox-fade-enter-active {
  transition: opacity 0.2s ease-in;
}

.my-ui-messagebox-fade-leave-active {
  transition: opacity 0.2s ease-out;
}

.ui-message-box {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);

  .ui-message-box-wrapper {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 420px;
    height: 200px;
    margin: -100px 0 0 -210px;
    background-color: #fff;
    border-radius: 3px;

    .message-box-title {
      padding: 15px 15px 10px;

      h1 {
        display: inline-block;
        font-size: 18px;
      }

      .fa-close {
        float: right;
        font-size: 12px;
        color: #999;
      }
    }

    .message-box-content {
      padding: 10px 15px;

      p {
        font-size: 14px;
      }
    }

    .message-box-btn-group {
      position: absolute;
      left: 0;
      bottom: 0;
      width: 100%;
      padding: 5px 15px 15px;
      box-sizing: border-box;

      button {
        float: right;
      }
    }
  }

  .btn {
    height: 32px;
    padding: 8px 15px;
    border-radius: 3px;
    margin-left: 10px;
    border: none;
    outline: none;

    &.btn-primary {
      background-color: #409eff;
      color: #fff;
    }

    &.btn-default {
      background-color: #fff;
      border: 1px solid #ddd;
    }
  }

  .message-input {
    border: none;
    outline: none;
    width: 100%;
    height: 32px;
    border: 1px solid #ddd;
    box-sizing: border-box;
  }
}
</style>
